package com.tlxch.controller.general;
import com.tlxch.dto.LearnModeldto;
import com.tlxch.dto.Mentaldto;
import com.tlxch.dto.Shabitdto;
import com.tlxch.result.Result;
import com.tlxch.utils.MentalHealthAssessor;
import com.tlxch.utils.StudentEvaluationUtils;
import com.tlxch.vo.AssessMsgvo;
import com.tlxch.vo.AssessMsgvo2;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/evaluation")
@Api(tags="评估学生相关接口")
@Slf4j
public class EvaluationAlgorithmController {

    @PostMapping("/learn")
    @ApiOperation("学习评估")
    public Result<List<AssessMsgvo>> evaluateStudent(@RequestBody List<LearnModeldto> request) {
        List<AssessMsgvo> assessMsgvos = new ArrayList<>();
        for (LearnModeldto learnModeldto : request){
            for (Shabitdto sh:learnModeldto.getShabitdtos()){
                if (sh.getShCreatetime() != null && !sh.getShCreatetime().isEmpty()) {
                    try {
                        // 尝试解析日期
                        LocalDate.parse(sh.getShCreatetime(), DateTimeFormatter.ISO_LOCAL_DATE);
                    } catch (Exception e) {
                        // 如果解析失败，设置默认值（当前日期）
                        sh.setShCreatetime(LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE));
                    }
                }
                // 调用评估工具类
                AssessMsgvo assessMsgvo = StudentEvaluationUtils.getInstance().evaluateStudent(learnModeldto);
                assessMsgvo.setSSid(learnModeldto.getSSid());
                assessMsgvo.setSName(learnModeldto.getSName());
                assessMsgvos.add(assessMsgvo);
            }
        }
        return Result.success(assessMsgvos);
    }
    @PostMapping("/mental")
    @ApiOperation("心理评估")
    public Result<List<AssessMsgvo2>> Mental(@RequestBody List<Mentaldto> textlist) {
        List<AssessMsgvo2> assessMsgvos = new ArrayList<>();
        // 使用工具类分析文本
        for (Mentaldto text : textlist){
            MentalHealthAssessor.AssessmentResult assessResult = MentalHealthAssessor.analyzeText(text.getText());
            AssessMsgvo2 assessMsgvo = new AssessMsgvo2();
            assessMsgvo.setSName(text.getSName());
            assessMsgvo.setSSid(text.getSSid());
            assessMsgvo.setTsScore(assessResult.getTotalScore());
            assessMsgvo.setTsContent(assessResult.getIssueScores());
            assessMsgvo.setTsLevel(assessResult.getRiskLevel());
            assessMsgvos.add(assessMsgvo);
        }
        return Result.success(assessMsgvos);
    }
}